//
//  ActivityViewController.m
//  HomeIn
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 ZZP. All rights reserved.
//

#import "ActivityViewController.h"
#import "ACModel.h"
#import "ACLLModel.h"
#import "ACCollectionViewcell.h"
#import "ACSViewController.h"
@interface ActivityViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *ACCollection;
@property(nonatomic,strong)NSMutableArray *allArray;
@property(nonatomic,strong)NSMutableArray *allFCArray;
@property(nonatomic,strong)ACModel *acModel;
@end

@implementation ActivityViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.allArray = [NSMutableArray array];
    self.allFCArray = [NSMutableArray array];
    //
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.minimumLineSpacing = 5;
    flowLayout.itemSize = CGSizeMake(90 * WIDTHSCALE, 90 *HEIGHTSCALE);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 10);
    //
    self.ACCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
    self.ACCollection.backgroundColor = [UIColor wheatColor];
    //协议
    self.ACCollection.delegate = self;
    self.ACCollection.dataSource = self;
    [self.view addSubview:self.ACCollection];
    //注测cell
    [self.ACCollection registerClass:[ACCollectionViewcell class] forCellWithReuseIdentifier:@"cell"];
    //注册表头
    [self.ACCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    flowLayout.headerReferenceSize=CGSizeMake(WIDTH, 60 * HEIGHTSCALE);
    [self AFNWroking];

}
-(void)AFNWroking
{
    NSString *url = [NSString stringWithFormat:@"http://api.bohejiaju.com/v1/channel_groups/all"];
    [BlockGetAFNYiBu GetAfnURLWithString:url resultBlock:^(id result)
    {
        NSMutableDictionary *BigDic = (NSMutableDictionary *)result;
        NSMutableDictionary *dicD = [BigDic objectForKey:@"data"];
        NSMutableArray *array = [dicD objectForKey:@"channel_groups"];
        for (NSMutableDictionary *dic in array)
        {
            self.acModel = [[ACModel alloc]init];
            [self.acModel setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:self.acModel];
            NSMutableArray *arrayFC = [dic objectForKey:@"channels"];
            for (NSMutableDictionary *dicFC in arrayFC)
            {
                ACLLModel *modelFC = [[ACLLModel alloc]init];
                modelFC.icon_url = [dicFC objectForKey:@"icon_url"];
                modelFC.name = [dicFC objectForKey:@"name"];
                modelFC.item_count = [dicFC objectForKey:@"item_count"];
                modelFC.id = [[dicFC objectForKey:@"id"]integerValue];
                //
                [self.allFCArray addObject:modelFC];
            }
        }
        [self.ACCollection reloadData];
    } errorBlcok:^(NSError *error)
    {
    }];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionVi
{
    
    return self.allArray.count;
}
//
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0)
    {
        ACModel *acmodel = self.allArray[0];
      return   acmodel.channels.count;
    }
    else
    {
        ACModel *acmodel = self.allArray[1];
        return   acmodel.channels.count;
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ACCollectionViewcell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [ UIColor clearColor];
    ACLLModel *model = [self.allFCArray objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    ACLLModel *model = [self.allFCArray objectAtIndex:indexPath.row];
    ACSViewController *acsVC = [[ACSViewController alloc]init];
    acsVC.ID = model.id;
    [self.navigationController pushViewController:acsVC animated:YES];
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 90 * HEIGHTSCALE;

}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableName = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    UILabel *labelName = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, WIDTH, 30 * HEIGHTSCALE)];
    labelName.font = [UIFont fontWithName:@"Arial" size:14];
    
    labelName.text = self.acModel.name;
    [reusableName addSubview:labelName];
    
    return reusableName;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
